package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCommentService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wanghaotian
 * time:2022/12/21,0021 12:55
 * description:
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/admin/comment")
public class CommentController {

    @Autowired
    private DtsBrandService brandService;

    @Autowired
    private DtsCommentService commentService;

    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(value = "limit",required = false,defaultValue = "20") Integer limit,
            Integer userId,Integer valueId,String sort,String order
    ){
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds = null;
        if (subject.hasRole("品牌制造商")) {
            brandIds = brandService.findBrandIds(admin.getId());
        }
        IPage<DtsComment> iPage = commentService.findPage(page,limit,userId,valueId,sort,order,brandIds);
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsComment comment){
        commentService.delete(comment);
        return ResponseUtil.ok();
    }
}
